<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use  App\Models\WeixinGroup;

use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;


class WeixinGroupController extends Controller
{
    public function create(Request $request)
    {
        $input = Input::only('name', 'remark','type');
        $validator = Validator::make($input, [        
            'name'      => 'required|string',
            'remark'     => 'nullable|string',
            'type'     => 'required|in:weixin,friend,group',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $data['remark'] = $input['remark']??'';
        $data['name'] = $input['name'];
        $data['type'] = $input['type'];
        $where['name'] = $input['name'];
        $exist = WeixinGroup::where($where)->first();
        if($exist){
            return $this->returnError([],409,'分组名称已存在');
        }else{
            WeixinGroup::create($data);
        }
        return $this->returnOk();
    }

    public function list()
    {
        $input = Input::only('str','type');
        $validator = Validator::make($input, [   
            'str'      => 'nullable|string',
            'type'     => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $group = WeixinGroup::where('id','>',0);
        if(isset($input['str'])){
            $group->where('name',$input['str']);
        } 
        if(isset($input['type'])){
            $group->where('type',$input['type']);
        } 
        $data = $group->get();
        return $this->returnOk($data);
    }

    public function update()
    {
        $input = Input::only('id','name', 'remark','type');
        $validator = Validator::make($input, [   
            'id'      => 'required',
            'name'      => 'required|string',
            'remark'     => 'nullable|string',
            'type'     => 'required|in:weixin,friend,group',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
       
        $where['id'] = $input['id'];
        $exist = WeixinGroup::where($where)->first();
        if($exist){
            $exist->name = $input['name'];
            $exist->remark = $input['remark']??'';
            $exist->type = $input['type'];
            $exist->save();
        }else{
            return $this->returnError([],409,'分组不存在');
        }
        return $this->returnOk();
    }

    public function delete()
    {
        $input = Input::only('id');
        $validator = Validator::make($input, [   
            'id'      => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
       
        $where['id'] = $input['id'];
        $exist = WeixinGroup::where($where)->first();
        if($exist){
            $exist->delete();
        }else{
            return $this->returnError([],409,'分组不存在');
        }
        return $this->returnOk();
    }
}